package courier;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.ejb.EJB;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import entity.Orders;
import facade.OrdersFacadeLocal;

/**
 *
 * @author Andriy
 */
public class courier extends HttpServlet {

    @EJB
    private OrdersFacadeLocal ofl;
    private String sortBy;

    public courier() {
        this.sortBy = "IdOrder";
    }

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            int page = 0;
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page")) - 1;
            }
            int[] a = {10 * page + 1, 10 * page + 10};
            List<Orders> ord;
            if (request.getParameter("sort") != null) {
                sortBy = request.getParameter("sort");
            }
            ord = ofl.findRangeSortBy(a, "shipped", sortBy);
            request.setAttribute("ordersList", ord);
            request.setAttribute("count", ofl.count("shipped"));
            RequestDispatcher dispatcher;
            dispatcher = getServletContext().getRequestDispatcher("/courierDboardMain.jsp");
            dispatcher.forward(request, response);
            /* TODO output your page here. You may use following sample code. */
        } finally {
            //out.println("zdfczc");           
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            if (request.getParameter("idOrderDel") != null) {
                Orders order = ofl.find(Integer.parseInt(request.getParameter("idOrderDel")));
                request.setAttribute("order", order);

                RequestDispatcher dispatcher;
                dispatcher = getServletContext().getRequestDispatcher("/orderDetail.jsp");
                dispatcher.forward(request, response);
            } else {
                processRequest(request, response);
            }
            /* TODO output your page here. You may use following sample code. */
        } finally {
            out.close();
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
                    if (request.getParameter("idOrderDeliver") != null) {
                        if (ofl.setDelivered(Integer.parseInt(request.getParameter("idOrderDeliver"))) == 1) {
                            response.sendRedirect("/Delta-war/courier");
                        } else {
                            request.setAttribute("error", "No such order!");
                        }
                    
                    //request.removeAttribute("idOrderDeliver");
                } /* TODO output your page here. You may use following sample code. */
        } finally {
            out.close();
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
